import { _decorator, AudioClip, Enum } from "cc";


const { ccclass, property } = _decorator;
export enum SoundType {

}
export class Sound {
    path: string;
    isEffect: boolean;
}
export class SM {

    private static soundMap: Map<string, Sound> = new Map<string, Sound>();
    private static audioMap: Map<string, AudioClip> = new Map<string, AudioClip>();
    private static musicMap: Map<AudioClip, number> = new Map<AudioClip, number>();
    private static effectMap: Map<AudioClip, number> = new Map<AudioClip, number>();
    private static musicVolume: number = 0.8;
    private static effectVolume: number = 0.8;

    public static init() {
        Enum.getList(SoundType).forEach(e => {
            this.registerSound(e.value);
        })
    }
    private static _activeMusic: boolean = true;
    public static get activeMusic() { return this._activeMusic; }
    static switchMusic(active: boolean) {
        this._activeMusic = active;
        if (this.activeMusic && this.curMusic)
            this.playMusicCore(this.curMusic, this.curMusicVolume);
        else if (!this.activeMusic) {
            this.stopMusicCore();
        }
    }
    private static _activeEffect: boolean = true;
    public static get activeEffect() { return this._activeEffect; }
    static switchEffect(active: boolean) {
        this._activeEffect = active;
        if (!this.activeEffect) {
            this.stopEffect();
        }
        else {
        }
    }
    public static registerSound(type: string, isEffect: boolean = false) {
        let path = `sound/${type}`;
        let sound: Sound = new Sound;
        sound.path = path;
        sound.isEffect = isEffect;
        this.soundMap.set(type, sound);
    }

    // public static async getClip(type: SoundType): Promise<AudioClip | undefined> {
    //     let audio = this.audioMap.get(type);
    //     if (!audio) {
    //         let sound = this.soundMap.get(type);
    //         if (sound == null) {
    //             console.error("type is not register: " + type + "  " + sound + "  " + this.soundMap.size);
    //             return;
    //         }
    //         audio = await loadRes(AudioClip, sound.path);
    //     }
    //     return audio;
    // }
    private static curMusic: SoundType;
    private static curMusicVolume: number;
    // public static async playMusic(type: SoundType, volume: number = 1) {
    //     this.curMusic = type;
    //     this.curMusicVolume = volume;
    //     if (!this._activeMusic)
    //         return;
    //     else
    //         this.playMusicCore(type, volume);
    // }
    // public static async playClip(clip: AudioClip, loop: boolean = true, volume: number = 1) {
    //     if (!this._activeMusic)
    //         return;
    //     this.stopMusicCore();
    //     let state = AudioManager.getState(this.musicMap.get(clip));
    //     if (state == AudioManager.AudioState.PLAYING) return;
    //     const id = AudioManager.play(clip, loop, this.musicVolume * volume);
    //     this.musicMap.set(clip, id);
    // }
    private static async playMusicCore(type: SoundType, volume: number = 1) {
        //     this.stopMusicCore();
        //     let clip = await this.getClip(type);
        //     let state = AudioManager.getState(this.musicMap.get(clip));
        //     if (state == AudioManager.AudioState.PLAYING) return;
        //     const id = AudioManager.play(clip, true, this.musicVolume * volume);
        //     this.musicMap.set(clip, id);
    }

    // public static async playEffect(type: SoundType, loop: boolean = false, volume: number = 1) {
    //     if (!this.activeEffect)
    //         return;
    //     console.log("play sound effect :" + type + "  " + loop + "  " + volume);
    //     let clip = await this.getClip(type);
    //     const id = AudioManager.play(clip, loop, this.effectVolume * volume);
    //     this.effectMap.set(clip, id);
    //     if (!loop) AudioManager.setFinishCallback(id, () => this.effectMap.delete(clip));
    // }

    // public static async playEffectForId(type: SoundType, volume: number = 1) {
    //     if (!this.activeEffect)
    //         return;
    //     console.log("play sound effect :" + type + "  " + volume);
    //     let clip = await this.getClip(type);
    //     const id = AudioManager.play(clip, true, this.effectVolume * volume);
    //     return id;
    // }
    // public static async stopEffectById(id: number) {
    //     AudioManager.stop(id);
    // }

    // public static async getMusicIsPlay(type: SoundType) {
    //     let sound = this.soundMap.get(type);
    //     const clip = await this.getClip(type);
    //     if (!clip) return false;
    //     if (sound.isEffect) {
    //         return this.effectMap.has(clip);
    //     } else {
    //         return this.musicMap.has(clip);
    //     }
    // }

    // public static async stop(type: SoundType) {
    //     let sound = this.soundMap.get(type);
    //     const clip = await this.getClip(type);

    //     if (!clip) return;
    //     let id = 0;
    //     if (sound.isEffect) {
    //         if (!this.effectMap.has(clip)) return;
    //         id = this.effectMap.get(clip);
    //         this.effectMap.delete(clip);
    //     } else {
    //         if (!this.musicMap.has(clip)) return;
    //         id = this.musicMap.get(clip);
    //         this.musicMap.delete(clip);
    //     }
    //     AudioManager.stop(id);
    // }

    // public static async stopMusic() {
    //     this.curMusic = null;
    //     this.stopMusicCore();
    // }
    private static async stopMusicCore() {
        //     this.musicMap.forEach((id, clip) => {
        //         AudioManager.stop(id);
        //     });
        //     this.musicMap.clear();

    }

    public static async stopEffect() {
        //     this.effectMap.forEach((id, clip) => {
        //         AudioManager.stop(id);
        //     });
        //     this.effectMap.clear();
    }

    // public static async stopAll() {
    //     AudioManager.stopAll();
    //     this.soundMap.clear();
    //     this.musicMap.clear();
    // }

    // public static setVolume(type: SoundType, volume: number) {
    //     let clip = this.audioMap.get(type);
    //     let sound = this.soundMap.get(type);
    //     if (!clip) return;
    //     let id = 0;
    //     if (sound.isEffect) {
    //         if (!this.effectMap.has(clip)) return;
    //         id = this.effectMap.get(clip);
    //     } else {
    //         if (!this.musicMap.has(clip)) return;
    //         id = this.musicMap.get(clip);
    //     }
    //     AudioManager.setVolume(id, volume);
    // }

    // public static setAllVolume(volume: number) {
    //     this.setEffectVolume(volume);
    //     this.setMusicVolume(volume);
    // }

    // public static setMusicVolume(volume: number) {
    //     if (volume < 0) volume = 0;
    //     if (volume > 1) volume = 1;
    //     this.musicMap.forEach((id, clip) => {
    //         AudioManager.setVolume(id, volume);
    //     });
    //     this.musicVolume = volume;
    // }

    // public static setEffectVolume(volume: number) {
    //     if (volume < 0) volume = 0;
    //     if (volume > 1) volume = 1;
    //     this.effectMap.forEach((id, clip) => {
    //         AudioManager.setVolume(id, volume);
    //     });
    //     this.effectVolume = volume;
    // }
}
window['SM'] = SM;